package com.java.mybatis.controller;

import com.java.mybatis.entity.Goodstype;
import com.java.mybatis.service.GoodsService;
import com.java.mybatis.service.GoodstypeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * (Goodstype)表控制层
 *
 * @author makejava
 * @since 2021-01-15 17:10:28
 */
@Controller
@RequestMapping("/goodstype/")
public class GoodstypeController {
    /**
     * 服务对象
     */
    @Resource
    private GoodstypeService goodstypeService;

    @Resource
    private GoodsService goodsService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Goodstype selectOne(Integer id) {
        return this.goodstypeService.queryById(id);
    }

    /**
     * 查询所有货物分类的方法
     * @param model
     * @return
     */
    @RequestMapping("SelAllGoodsType")
    public String SelAllGoodsType(Model model){

        try {
            List<Goodstype> goodstypes = goodstypeService.selAllGoodsType();
            model.addAttribute("allgoodsType",goodstypes);
            Integer count = goodstypeService.selGoodsTypeCount();
            model.addAttribute("count",count);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "System/member-kiss";
    }

    /**
     * 启用的方法
     * @param id
     * @return
     */
    @RequestMapping("upGoodsTypeUp")
    public String upGoodsTypeUp(Integer id){
        try {
            goodstypeService.upGoodsTypeCodeUp(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/goodstype/SelAllGoodsType";
    }

    /**
     * 禁用的方法
     * @param id
     * @return
     */
    @RequestMapping("upGoodsTypeDonw")
    @ResponseBody
    public String upGoodsTypeDonw(Integer id, HttpServletResponse response){
       response.setContentType("UTF-8");
       response.setCharacterEncoding("UTF-8");
        try {
            Integer count = goodsService.selGoodsTypeCount(id);
            if(count==0){
                goodstypeService.upGoodsTypeCodeDown(id);
            }else {
                return "该分类中还有货物未出库，不能被禁用。";
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "禁用成功！";
    }

    @RequestMapping("upGoodsTpyeAll")
    @ResponseBody
    public String upGoodsTpyeAll(String str,HttpServletResponse response) throws Exception {
        response.setContentType("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String[] split = str.split(",");
        for (String s : split) {
            Integer count = goodsService.selGoodsTypeCount(Integer.parseInt(s));
            if(count==0){
                goodstypeService.upGoodsTypeCodeDown(Integer.parseInt(s));
            }else {
                return "选择的分类中含有货物未出库，不能被禁用，请重新选择。";
            }
        }
        return "禁用成功！";
    }
}